package cn.hdu.liang.typeinfo;

import cn.hdu.liang.typeinfo.factory.Factory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

class Part {
    public String toString() {
        return getClass().getSimpleName();
    }

    static List<Factory<? extends Part>> partFactories = new ArrayList<>();

    static {
        Collections.addAll(partFactories,
                new FuelFilter.Factory(),
                new AirFilter.Factory(),
                new CabinAirFilter.Factory(),
                new OilFilter.Factory(),
                new FanBelt.Factory(),
                new PowerSteeringBelt.Factory(),
                new GeneratorBelt.Factory()
        );
    }

    private static Random random = new Random(47);

    public static Part createRandom() {
        int n = random.nextInt(partFactories.size());
        return partFactories.get(n).create();
    }
}

class Filter extends Part {

}

class FuelFilter extends Filter {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<FuelFilter> {
        public FuelFilter create() {
            return new FuelFilter();
        }
    }
}

class AirFilter extends Filter {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<AirFilter> {
        public AirFilter create() {
            return new AirFilter();
        }
    }
}

class CabinAirFilter extends Filter {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<CabinAirFilter> {
        public CabinAirFilter create() {
            return new CabinAirFilter();
        }
    }
}

class OilFilter extends Filter {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<OilFilter> {
        public OilFilter create() {
            return new OilFilter();
        }
    }
}

class Belt extends Part {

}

class FanBelt extends Belt {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<FanBelt> {
        public FanBelt create() {
            return new FanBelt();
        }
    }
}

class GeneratorBelt extends Belt {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<GeneratorBelt> {
        public GeneratorBelt create() {
            return new GeneratorBelt();
        }
    }
}

class PowerSteeringBelt extends Belt {
    public static class Factory implements cn.hdu.liang.typeinfo.factory.Factory<PowerSteeringBelt> {
        public PowerSteeringBelt create() {
            return new PowerSteeringBelt();
        }
    }
}


public class RegisteredFactories {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++) {
            System.out.println(Part.createRandom());
        }
    }
}